<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Math Word Problem Tutorial &mdash; Graph4NLP v0.4.1 documentation</title><link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  <script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script src="../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Knowledge Graph Completion Tutorial" href="knowledge_graph_completion.html" />
    <link rel="prev" title="Semantic Parsing Tutorial" href="semantic_parsing.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../index.html" class="icon icon-home"> Graph4NLP
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption"><span class="caption-text">Get Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../welcome/installation.html">Install Graph4NLP</a></li>
</ul>
<p class="caption"><span class="caption-text">User Guide</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../guide/graphdata.html">Chapter 1. Graph Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../guide/dataset.html">Chapter 2. Dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../guide/construction.html">Chapter 3. Graph Construction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../guide/gnn.html">Chapter 4. Graph Encoder</a></li>
<li class="toctree-l1"><a class="reference internal" href="../guide/decoding.html">Chapter 5. Decoder</a></li>
<li class="toctree-l1"><a class="reference internal" href="../guide/classification.html">Chapter 6. Classification</a></li>
<li class="toctree-l1"><a class="reference internal" href="../guide/evaluation.html">Chapter 7. Evaluations and Loss components</a></li>
</ul>
<p class="caption"><span class="caption-text">Module API references</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../modules/data.html">graph4nlp.data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../modules/datasets.html">graph4nlp.datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../modules/graph_construction.html">graph4nlp.graph_construction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../modules/graph_embedding.html">graph4nlp.graph_embedding</a></li>
<li class="toctree-l1"><a class="reference internal" href="../modules/prediction.html">graph4nlp.prediction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../modules/loss.html">graph4nlp.loss</a></li>
<li class="toctree-l1"><a class="reference internal" href="../modules/evaluation.html">graph4nlp.evaluation</a></li>
</ul>
<p class="caption"><span class="caption-text">Tutorials</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="text_classification.html">Text Classification Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="semantic_parsing.html">Semantic Parsing Tutorial</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Math Word Problem Tutorial</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#introduction">Introduction</a></li>
<li class="toctree-l2"><a class="reference internal" href="#environment-setup">Environment setup</a></li>
<li class="toctree-l2"><a class="reference internal" href="#load-the-config-file">Load the config file</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#the-config-output">The config output</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#import-packages">Import packages</a></li>
<li class="toctree-l2"><a class="reference internal" href="#build-the-model">Build the model</a></li>
<li class="toctree-l2"><a class="reference internal" href="#run-and-get-results">Run and get results</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="knowledge_graph_completion.html">Knowledge Graph Completion Tutorial</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Graph4NLP</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
      <li>Math Word Problem Tutorial</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../_sources/tutorial/math_word_problem.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <div class="section" id="math-word-problem-tutorial">
<h1>Math Word Problem Tutorial<a class="headerlink" href="#math-word-problem-tutorial" title="Permalink to this headline">¶</a></h1>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>In this demo, we will have a closer look at how to apply <strong>Graph2Tree
model to the task of math word problem automatically solving</strong>. Math
word problem solving aims to infer reasonable equations from given
natural language problem descriptions. It is important for exploring
automatic solutions to mathematical problems and improving the reasoning
ability of neural networks. In this demo, we use the Graph4NLP library
to build a GNN-based math word problem (MWP) solving model.</p>
<p>The <strong>Graph2Tree</strong> model consists of:</p>
<ul class="simple">
<li><p>graph construction module (e.g., node embedding based dynamic graph)</p></li>
<li><p>graph embedding module (e.g., undirected GraphSage)</p></li>
<li><p>predictoin module (e.g., tree decoder with attention and copy
mechanisms)</p></li>
</ul>
<p>The full example can be downloaded from <a class="reference external" href="https://github.com/schenglee/Graph4nlp_demo/blob/main/demo_graph2tree/math_word_problem.ipynb">Math word problem notebook</a>.</p>
<p>As shown in the picture below, we firstly construct graph input from
problem description by syntactic parsing (CoreNLP) and then represent
the output equation with a hierarchical structure (Node “N” stands for
non-terminal node).</p>
<a class="reference internal image-reference" href="../_images/g2t.png"><img alt="../_images/g2t.png" src="../_images/g2t.png" style="height: 250px;" /></a>
<p>We will use the built-in Graph2Tree model APIs to build the model, and
evaluate it on the Mawps dataset.</p>
</div>
<div class="section" id="environment-setup">
<h2>Environment setup<a class="headerlink" href="#environment-setup" title="Permalink to this headline">¶</a></h2>
<ol class="arabic simple">
<li><p>Create virtual environment</p></li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="n">create</span> <span class="o">--</span><span class="n">name</span> <span class="n">graph4nlp</span> <span class="n">python</span><span class="o">=</span><span class="mf">3.7</span>
<span class="n">conda</span> <span class="n">activate</span> <span class="n">graph4nlp</span>
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li><p>Install <a class="reference external" href="https://github.com/graph4ai/graph4nlp">graph4nlp</a> library</p></li>
</ol>
<ul class="simple">
<li><p>Clone the github repo</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">clone</span> <span class="o">-</span><span class="n">b</span> <span class="n">stable</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">graph4ai</span><span class="o">/</span><span class="n">graph4nlp</span><span class="o">.</span><span class="n">git</span>
<span class="n">cd</span> <span class="n">graph4nlp</span>
</pre></div>
</div>
<ul class="simple">
<li><p>Then run <code class="docutils literal notranslate"><span class="pre">./configure</span></code> (or <code class="docutils literal notranslate"><span class="pre">./configure.bat</span></code> if you are using
Windows 10) to config your installation. The configuration program
will ask you to specify your CUDA version. If you do not have a GPU,
please choose ‘cpu’.</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">./</span><span class="n">configure</span>
</pre></div>
</div>
<ul class="simple">
<li><p>Finally, install the package</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">install</span>
</pre></div>
</div>
<ol class="arabic simple" start="3">
<li><p>Set up StanfordCoreNLP (for static graph construction only,
unnecessary for this demo because preprocessed data is provided)</p></li>
</ol>
<ul class="simple">
<li><p>Download <a class="reference external" href="https://stanfordnlp.github.io/CoreNLP/">StanfordCoreNLP</a></p></li>
<li><p>Go to the root folder and start the server</p></li>
</ul>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">java</span> <span class="o">-</span><span class="n">mx4g</span> <span class="o">-</span><span class="n">cp</span> <span class="s2">&quot;*&quot;</span> <span class="n">edu</span><span class="o">.</span><span class="n">stanford</span><span class="o">.</span><span class="n">nlp</span><span class="o">.</span><span class="n">pipeline</span><span class="o">.</span><span class="n">StanfordCoreNLPServer</span> <span class="o">-</span><span class="n">port</span> <span class="mi">9000</span> <span class="o">-</span><span class="n">timeout</span> <span class="mi">15000</span>
</pre></div>
</div>
</div>
<div class="section" id="load-the-config-file">
<h2>Load the config file<a class="headerlink" href="#load-the-config-file" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">graph4nlp.pytorch.modules.config</span> <span class="kn">import</span> <span class="n">get_basic_args</span>
<span class="kn">from</span> <span class="nn">graph4nlp.pytorch.modules.utils.config_utils</span> <span class="kn">import</span> <span class="n">update_values</span><span class="p">,</span> <span class="n">get_yaml_config</span>

<span class="k">def</span> <span class="nf">get_args</span><span class="p">():</span>
    <span class="n">config</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;dataset_yaml&#39;</span><span class="p">:</span> <span class="s2">&quot;./config.yaml&quot;</span><span class="p">,</span>
              <span class="s1">&#39;learning_rate&#39;</span><span class="p">:</span> <span class="mf">1e-3</span><span class="p">,</span>
              <span class="s1">&#39;gpuid&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
              <span class="s1">&#39;seed&#39;</span><span class="p">:</span> <span class="mi">123</span><span class="p">,</span>
              <span class="s1">&#39;init_weight&#39;</span><span class="p">:</span> <span class="mf">0.08</span><span class="p">,</span>
              <span class="s1">&#39;graph_type&#39;</span><span class="p">:</span> <span class="s1">&#39;static&#39;</span><span class="p">,</span>
              <span class="s1">&#39;weight_decay&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
              <span class="s1">&#39;max_epochs&#39;</span><span class="p">:</span> <span class="mi">20</span><span class="p">,</span>
              <span class="s1">&#39;min_freq&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
              <span class="s1">&#39;grad_clip&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span>
              <span class="s1">&#39;batch_size&#39;</span><span class="p">:</span> <span class="mi">20</span><span class="p">,</span>
              <span class="s1">&#39;share_vocab&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
              <span class="s1">&#39;pretrained_word_emb_name&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
              <span class="s1">&#39;pretrained_word_emb_url&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
              <span class="s1">&#39;pretrained_word_emb_cache_dir&#39;</span><span class="p">:</span> <span class="s2">&quot;.vector_cache&quot;</span><span class="p">,</span>
              <span class="s1">&#39;checkpoint_save_path&#39;</span><span class="p">:</span> <span class="s2">&quot;./checkpoint_save&quot;</span><span class="p">,</span>
              <span class="s1">&#39;beam_size&#39;</span><span class="p">:</span> <span class="mi">4</span>
              <span class="p">}</span>
    <span class="n">our_args</span> <span class="o">=</span> <span class="n">get_yaml_config</span><span class="p">(</span><span class="n">config</span><span class="p">[</span><span class="s1">&#39;dataset_yaml&#39;</span><span class="p">])</span>
    <span class="n">template</span> <span class="o">=</span> <span class="n">get_basic_args</span><span class="p">(</span><span class="n">graph_construction_name</span><span class="o">=</span><span class="n">our_args</span><span class="p">[</span><span class="s2">&quot;graph_construction_name&quot;</span><span class="p">],</span>
                              <span class="n">graph_embedding_name</span><span class="o">=</span><span class="n">our_args</span><span class="p">[</span><span class="s2">&quot;graph_embedding_name&quot;</span><span class="p">],</span>
                              <span class="n">decoder_name</span><span class="o">=</span><span class="n">our_args</span><span class="p">[</span><span class="s2">&quot;decoder_name&quot;</span><span class="p">])</span>
    <span class="n">update_values</span><span class="p">(</span><span class="n">to_args</span><span class="o">=</span><span class="n">template</span><span class="p">,</span> <span class="n">from_args_list</span><span class="o">=</span><span class="p">[</span><span class="n">our_args</span><span class="p">,</span> <span class="n">config</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">template</span>

<span class="c1"># show our config</span>
<span class="n">cfg_g2t</span> <span class="o">=</span> <span class="n">get_args</span><span class="p">()</span>
<span class="kn">from</span> <span class="nn">pprint</span> <span class="kn">import</span> <span class="n">pprint</span>
<span class="n">pprint</span><span class="p">(</span><span class="n">cfg_g2t</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="the-config-output">
<h3>The config output<a class="headerlink" href="#the-config-output" title="Permalink to this headline">¶</a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s1">&#39;batch_size&#39;</span><span class="p">:</span> <span class="mi">20</span><span class="p">,</span>
 <span class="s1">&#39;beam_size&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
 <span class="s1">&#39;checkpoint_save_path&#39;</span><span class="p">:</span> <span class="s1">&#39;./checkpoint_save&#39;</span><span class="p">,</span>
 <span class="s1">&#39;dataset_yaml&#39;</span><span class="p">:</span> <span class="s1">&#39;./config.yaml&#39;</span><span class="p">,</span>
 <span class="s1">&#39;decoder_args&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;rnn_decoder_private&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;max_decoder_step&#39;</span><span class="p">:</span> <span class="mi">35</span><span class="p">,</span>
                                          <span class="s1">&#39;max_tree_depth&#39;</span><span class="p">:</span> <span class="mi">8</span><span class="p">,</span>
                                          <span class="s1">&#39;use_input_feed&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
                                          <span class="s1">&#39;use_sibling&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span>
                  <span class="s1">&#39;rnn_decoder_share&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;attention_type&#39;</span><span class="p">:</span> <span class="s1">&#39;uniform&#39;</span><span class="p">,</span>
                                        <span class="s1">&#39;dropout&#39;</span><span class="p">:</span> <span class="mf">0.3</span><span class="p">,</span>
                                        <span class="s1">&#39;fuse_strategy&#39;</span><span class="p">:</span> <span class="s1">&#39;concatenate&#39;</span><span class="p">,</span>
                                        <span class="s1">&#39;graph_pooling_strategy&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                                        <span class="s1">&#39;hidden_size&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span>
                                        <span class="s1">&#39;input_size&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span>
                                        <span class="s1">&#39;rnn_emb_input_size&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span>
                                        <span class="s1">&#39;rnn_type&#39;</span><span class="p">:</span> <span class="s1">&#39;lstm&#39;</span><span class="p">,</span>
                                        <span class="s1">&#39;teacher_forcing_rate&#39;</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">,</span>
                                        <span class="s1">&#39;use_copy&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
                                        <span class="s1">&#39;use_coverage&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">}},</span>
 <span class="s1">&#39;decoder_name&#39;</span><span class="p">:</span> <span class="s1">&#39;stdtree&#39;</span><span class="p">,</span>
 <span class="s1">&#39;gpuid&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
 <span class="s1">&#39;grad_clip&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span>
 <span class="s1">&#39;graph_construction_args&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;graph_construction_private&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;as_node&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
                                                            <span class="s1">&#39;edge_strategy&#39;</span><span class="p">:</span> <span class="s1">&#39;homogeneous&#39;</span><span class="p">,</span>
                                                            <span class="s1">&#39;merge_strategy&#39;</span><span class="p">:</span> <span class="s1">&#39;tailhead&#39;</span><span class="p">,</span>
                                                            <span class="s1">&#39;sequential_link&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">},</span>
                             <span class="s1">&#39;graph_construction_share&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;graph_type&#39;</span><span class="p">:</span> <span class="s1">&#39;dependency&#39;</span><span class="p">,</span>
                                                          <span class="s1">&#39;port&#39;</span><span class="p">:</span> <span class="mi">9000</span><span class="p">,</span>
                                                          <span class="s1">&#39;root_dir&#39;</span><span class="p">:</span> <span class="s1">&#39;./data&#39;</span><span class="p">,</span>
                                                          <span class="s1">&#39;share_vocab&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
                                                          <span class="s1">&#39;thread_number&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
                                                          <span class="s1">&#39;timeout&#39;</span><span class="p">:</span> <span class="mi">15000</span><span class="p">,</span>
                                                          <span class="s1">&#39;topology_subdir&#39;</span><span class="p">:</span> <span class="s1">&#39;DependencyGraph&#39;</span><span class="p">},</span>
                             <span class="s1">&#39;node_embedding&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;connectivity_ratio&#39;</span><span class="p">:</span> <span class="mf">0.05</span><span class="p">,</span>
                                                <span class="s1">&#39;embedding_style&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;bert_lower_case&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                                                                    <span class="s1">&#39;bert_model_name&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                                                                    <span class="s1">&#39;emb_strategy&#39;</span><span class="p">:</span> <span class="s1">&#39;w2v_bilstm&#39;</span><span class="p">,</span>
                                                                    <span class="s1">&#39;num_rnn_layers&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
                                                                    <span class="s1">&#39;single_token_item&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">},</span>
                                                <span class="s1">&#39;epsilon_neigh&#39;</span><span class="p">:</span> <span class="mf">0.5</span><span class="p">,</span>
                                                <span class="s1">&#39;fix_bert_emb&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
                                                <span class="s1">&#39;fix_word_emb&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
                                                <span class="s1">&#39;hidden_size&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span>
                                                <span class="s1">&#39;input_size&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span>
                                                <span class="s1">&#39;num_heads&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
                                                <span class="s1">&#39;rnn_dropout&#39;</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">,</span>
                                                <span class="s1">&#39;sim_metric_type&#39;</span><span class="p">:</span> <span class="s1">&#39;weighted_cosine&#39;</span><span class="p">,</span>
                                                <span class="s1">&#39;smoothness_ratio&#39;</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">,</span>
                                                <span class="s1">&#39;sparsity_ratio&#39;</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">,</span>
                                                <span class="s1">&#39;top_k_neigh&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                                                <span class="s1">&#39;word_dropout&#39;</span><span class="p">:</span> <span class="mf">0.1</span><span class="p">}},</span>
 <span class="s1">&#39;graph_construction_name&#39;</span><span class="p">:</span> <span class="s1">&#39;dependency&#39;</span><span class="p">,</span>
 <span class="s1">&#39;graph_embedding_args&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;graph_embedding_private&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;activation&#39;</span><span class="p">:</span> <span class="s1">&#39;relu&#39;</span><span class="p">,</span>
                                                      <span class="s1">&#39;aggregator_type&#39;</span><span class="p">:</span> <span class="s1">&#39;lstm&#39;</span><span class="p">,</span>
                                                      <span class="s1">&#39;bias&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
                                                      <span class="s1">&#39;norm&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                                                      <span class="s1">&#39;use_edge_weight&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span>
                          <span class="s1">&#39;graph_embedding_share&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;attn_drop&#39;</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">,</span>
                                                    <span class="s1">&#39;direction_option&#39;</span><span class="p">:</span> <span class="s1">&#39;undirected&#39;</span><span class="p">,</span>
                                                    <span class="s1">&#39;feat_drop&#39;</span><span class="p">:</span> <span class="mf">0.0</span><span class="p">,</span>
                                                    <span class="s1">&#39;hidden_size&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span>
                                                    <span class="s1">&#39;input_size&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">,</span>
                                                    <span class="s1">&#39;num_layers&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
                                                    <span class="s1">&#39;output_size&#39;</span><span class="p">:</span> <span class="mi">300</span><span class="p">}},</span>
 <span class="s1">&#39;graph_embedding_name&#39;</span><span class="p">:</span> <span class="s1">&#39;graphsage&#39;</span><span class="p">,</span>
 <span class="s1">&#39;graph_type&#39;</span><span class="p">:</span> <span class="s1">&#39;static&#39;</span><span class="p">,</span>
 <span class="s1">&#39;init_weight&#39;</span><span class="p">:</span> <span class="mf">0.08</span><span class="p">,</span>
 <span class="s1">&#39;learning_rate&#39;</span><span class="p">:</span> <span class="mf">0.001</span><span class="p">,</span>
 <span class="s1">&#39;max_epochs&#39;</span><span class="p">:</span> <span class="mi">20</span><span class="p">,</span>
 <span class="s1">&#39;min_freq&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
 <span class="s1">&#39;pretrained_word_emb_cache_dir&#39;</span><span class="p">:</span> <span class="s1">&#39;.vector_cache&#39;</span><span class="p">,</span>
 <span class="s1">&#39;pretrained_word_emb_name&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
 <span class="s1">&#39;pretrained_word_emb_url&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
 <span class="s1">&#39;seed&#39;</span><span class="p">:</span> <span class="mi">123</span><span class="p">,</span>
 <span class="s1">&#39;share_vocab&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
 <span class="s1">&#39;weight_decay&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="import-packages">
<h2>Import packages<a class="headerlink" href="#import-packages" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">argparse</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">torch.optim</span> <span class="k">as</span> <span class="nn">optim</span>
<span class="kn">from</span> <span class="nn">torch.utils.data</span> <span class="kn">import</span> <span class="n">DataLoader</span>
<span class="kn">from</span> <span class="nn">tqdm.notebook</span> <span class="kn">import</span> <span class="n">tqdm</span>

<span class="kn">from</span> <span class="nn">graph4nlp.pytorch.data.data</span> <span class="kn">import</span> <span class="n">to_batch</span>
<span class="kn">from</span> <span class="nn">graph4nlp.pytorch.datasets.mawps</span> <span class="kn">import</span> <span class="n">MawpsDatasetForTree</span>
<span class="kn">from</span> <span class="nn">graph4nlp.pytorch.modules.graph_construction</span> <span class="kn">import</span> <span class="n">DependencyBasedGraphConstruction</span>
<span class="kn">from</span> <span class="nn">graph4nlp.pytorch.modules.graph_embedding</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">graph4nlp.pytorch.models.graph2tree</span> <span class="kn">import</span> <span class="n">Graph2Tree</span>
<span class="kn">from</span> <span class="nn">graph4nlp.pytorch.modules.utils.tree_utils</span> <span class="kn">import</span> <span class="n">Tree</span><span class="p">,</span> <span class="n">prepare_oov</span>

<span class="kn">from</span> <span class="nn">utils</span> <span class="kn">import</span> <span class="n">convert_to_string</span><span class="p">,</span> <span class="n">compute_tree_accuracy</span>
</pre></div>
</div>
</div>
<div class="section" id="build-the-model">
<h2>Build the model<a class="headerlink" href="#build-the-model" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Mawps</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">opt</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Mawps</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opt</span> <span class="o">=</span> <span class="n">opt</span>

        <span class="n">seed</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;seed&quot;</span><span class="p">]</span>
        <span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
        <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
        <span class="n">torch</span><span class="o">.</span><span class="n">manual_seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;gpuid&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">device</span><span class="p">(</span><span class="s2">&quot;cpu&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">device</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">device</span><span class="p">(</span><span class="s2">&quot;cuda:</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;gpuid&quot;</span><span class="p">]))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">use_copy</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;decoder_args&quot;</span><span class="p">][</span><span class="s2">&quot;rnn_decoder_share&quot;</span><span class="p">][</span><span class="s2">&quot;use_copy&quot;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">use_share_vocab</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;graph_construction_args&quot;</span><span class="p">][</span><span class="s2">&quot;graph_construction_share&quot;</span><span class="p">][</span><span class="s2">&quot;share_vocab&quot;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data_dir</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;graph_construction_args&quot;</span><span class="p">][</span><span class="s2">&quot;graph_construction_share&quot;</span><span class="p">][</span><span class="s2">&quot;root_dir&quot;</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_build_dataloader</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_build_model</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_build_optimizer</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_build_dataloader</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">para_dic</span> <span class="o">=</span>  <span class="p">{</span><span class="s1">&#39;root_dir&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_dir</span><span class="p">,</span>
                    <span class="s1">&#39;word_emb_size&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;graph_construction_args&quot;</span><span class="p">][</span><span class="s2">&quot;node_embedding&quot;</span><span class="p">][</span><span class="s2">&quot;input_size&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;topology_builder&#39;</span><span class="p">:</span> <span class="n">DependencyBasedGraphConstruction</span><span class="p">,</span>
                    <span class="s1">&#39;topology_subdir&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;graph_construction_args&quot;</span><span class="p">][</span><span class="s2">&quot;graph_construction_share&quot;</span><span class="p">][</span><span class="s2">&quot;topology_subdir&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;edge_strategy&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;graph_construction_args&quot;</span><span class="p">][</span><span class="s2">&quot;graph_construction_private&quot;</span><span class="p">][</span><span class="s2">&quot;edge_strategy&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;graph_type&#39;</span><span class="p">:</span> <span class="s1">&#39;static&#39;</span><span class="p">,</span>
                    <span class="s1">&#39;dynamic_graph_type&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;graph_construction_args&quot;</span><span class="p">][</span><span class="s2">&quot;graph_construction_share&quot;</span><span class="p">][</span><span class="s2">&quot;graph_type&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;share_vocab&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_share_vocab</span><span class="p">,</span>
                    <span class="s1">&#39;enc_emb_size&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;graph_construction_args&quot;</span><span class="p">][</span><span class="s2">&quot;node_embedding&quot;</span><span class="p">][</span><span class="s2">&quot;input_size&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;dec_emb_size&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;decoder_args&quot;</span><span class="p">][</span><span class="s2">&quot;rnn_decoder_share&quot;</span><span class="p">][</span><span class="s2">&quot;input_size&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;dynamic_init_topology_builder&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                    <span class="s1">&#39;min_word_vocab_freq&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;min_freq&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;pretrained_word_emb_name&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;pretrained_word_emb_name&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;pretrained_word_emb_url&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;pretrained_word_emb_url&quot;</span><span class="p">],</span>
                    <span class="s1">&#39;pretrained_word_emb_cache_dir&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;pretrained_word_emb_cache_dir&quot;</span><span class="p">]</span>
                    <span class="p">}</span>

        <span class="n">dataset</span> <span class="o">=</span> <span class="n">MawpsDatasetForTree</span><span class="p">(</span><span class="o">**</span><span class="n">para_dic</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">train_data_loader</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">dataset</span><span class="o">.</span><span class="n">train</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;batch_size&quot;</span><span class="p">],</span> <span class="n">shuffle</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                            <span class="n">num_workers</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                                            <span class="n">collate_fn</span><span class="o">=</span><span class="n">dataset</span><span class="o">.</span><span class="n">collate_fn</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">test_data_loader</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">dataset</span><span class="o">.</span><span class="n">test</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">shuffle</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">num_workers</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                                           <span class="n">collate_fn</span><span class="o">=</span><span class="n">dataset</span><span class="o">.</span><span class="n">collate_fn</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">valid_data_loader</span> <span class="o">=</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">dataset</span><span class="o">.</span><span class="n">val</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">shuffle</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">num_workers</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                                          <span class="n">collate_fn</span><span class="o">=</span><span class="n">dataset</span><span class="o">.</span><span class="n">collate_fn</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vocab_model</span> <span class="o">=</span> <span class="n">dataset</span><span class="o">.</span><span class="n">vocab_model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">src_vocab</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">vocab_model</span><span class="o">.</span><span class="n">in_word_vocab</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tgt_vocab</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">vocab_model</span><span class="o">.</span><span class="n">out_word_vocab</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">share_vocab</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">vocab_model</span><span class="o">.</span><span class="n">share_vocab</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_share_vocab</span> <span class="k">else</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">_build_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;For encoder-decoder&#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">Graph2Tree</span><span class="o">.</span><span class="n">from_args</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">,</span> <span class="n">vocab_model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">vocab_model</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">init</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;init_weight&quot;</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_build_optimizer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">optim_state</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;learningRate&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;learning_rate&quot;</span><span class="p">],</span> <span class="s2">&quot;weight_decay&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;weight_decay&quot;</span><span class="p">]}</span>
        <span class="n">parameters</span> <span class="o">=</span> <span class="p">[</span><span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">parameters</span><span class="p">()</span> <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">requires_grad</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span> <span class="o">=</span> <span class="n">optim</span><span class="o">.</span><span class="n">Adam</span><span class="p">(</span><span class="n">parameters</span><span class="p">,</span> <span class="n">lr</span><span class="o">=</span><span class="n">optim_state</span><span class="p">[</span><span class="s1">&#39;learningRate&#39;</span><span class="p">],</span> <span class="n">weight_decay</span><span class="o">=</span><span class="n">optim_state</span><span class="p">[</span><span class="s1">&#39;weight_decay&#39;</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">train_epoch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">epoch</span><span class="p">):</span>
        <span class="n">loss_to_print</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">num_batch</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">train_data_loader</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">step</span><span class="p">,</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">tqdm</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">train_data_loader</span><span class="p">),</span> <span class="n">desc</span><span class="o">=</span><span class="sa">f</span><span class="s1">&#39;Epoch </span><span class="si">{epoch:02d}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">total</span><span class="o">=</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">train_data_loader</span><span class="p">)):</span>
            <span class="n">batch_graph</span><span class="p">,</span> <span class="n">batch_tree_list</span><span class="p">,</span> <span class="n">batch_original_tree_list</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;graph_data&#39;</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;dec_tree_batch&#39;</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;original_dec_tree_batch&#39;</span><span class="p">]</span>
            <span class="n">batch_graph</span> <span class="o">=</span> <span class="n">batch_graph</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">zero_grad</span><span class="p">()</span>
            <span class="n">oov_dict</span> <span class="o">=</span> <span class="n">prepare_oov</span><span class="p">(</span>
                <span class="n">batch_graph</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">src_vocab</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">)</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_copy</span> <span class="k">else</span> <span class="kc">None</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_copy</span><span class="p">:</span>
                <span class="n">batch_tree_list_refined</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">batch_original_tree_list</span><span class="p">:</span>
                    <span class="n">tgt_list</span> <span class="o">=</span> <span class="n">oov_dict</span><span class="o">.</span><span class="n">get_symbol_idx_for_list</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
                    <span class="n">tgt_tree</span> <span class="o">=</span> <span class="n">Tree</span><span class="o">.</span><span class="n">convert_to_tree</span><span class="p">(</span><span class="n">tgt_list</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">tgt_list</span><span class="p">),</span> <span class="n">oov_dict</span><span class="p">)</span>
                    <span class="n">batch_tree_list_refined</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tgt_tree</span><span class="p">)</span>
            <span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="n">batch_graph</span><span class="p">,</span> <span class="n">batch_tree_list_refined</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_copy</span> <span class="k">else</span> <span class="n">batch_tree_list</span><span class="p">,</span> <span class="n">oov_dict</span><span class="o">=</span><span class="n">oov_dict</span><span class="p">)</span>
            <span class="n">loss</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>
            <span class="n">torch</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">clip_grad_value_</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">parameters</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;grad_clip&quot;</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">optimizer</span><span class="o">.</span><span class="n">step</span><span class="p">()</span>
            <span class="n">loss_to_print</span> <span class="o">+=</span> <span class="n">loss</span>
        <span class="k">return</span> <span class="n">loss_to_print</span><span class="o">/</span><span class="n">num_batch</span>

    <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">best_acc</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
        <span class="n">best_model</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;-------------</span><span class="se">\n</span><span class="s2">Starting training.&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;max_epochs&quot;</span><span class="p">]</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">train</span><span class="p">()</span>
            <span class="n">loss_to_print</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">train_epoch</span><span class="p">(</span><span class="n">epoch</span><span class="p">)</span>
            <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;epochs = </span><span class="si">{}</span><span class="s2">, train_loss = </span><span class="si">{:.3f}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">epoch</span><span class="p">,</span> <span class="n">loss_to_print</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">epoch</span> <span class="o">&gt;</span> <span class="mi">15</span><span class="p">:</span>
                <span class="n">val_acc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">eval</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;val&quot;</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">val_acc</span> <span class="o">&gt;</span> <span class="n">best_acc</span><span class="p">:</span>
                    <span class="n">best_acc</span> <span class="o">=</span> <span class="n">val_acc</span>
                    <span class="n">best_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eval</span><span class="p">(</span><span class="n">best_model</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;test&quot;</span><span class="p">)</span>
        <span class="n">best_model</span><span class="o">.</span><span class="n">save_checkpoint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;checkpoint_save_path&quot;</span><span class="p">],</span> <span class="s2">&quot;best.pt&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">eval</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;val&quot;</span><span class="p">):</span>
        <span class="n">model</span><span class="o">.</span><span class="n">eval</span><span class="p">()</span>
        <span class="n">reference_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">candidate_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">data_loader</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">test_data_loader</span> <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;test&quot;</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">valid_data_loader</span>
        <span class="k">for</span> <span class="n">data</span> <span class="ow">in</span> <span class="n">tqdm</span><span class="p">(</span><span class="n">data_loader</span><span class="p">,</span> <span class="n">desc</span><span class="o">=</span><span class="s2">&quot;Eval: &quot;</span><span class="p">):</span>
            <span class="n">eval_input_graph</span><span class="p">,</span> <span class="n">batch_tree_list</span><span class="p">,</span> <span class="n">batch_original_tree_list</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;graph_data&#39;</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;dec_tree_batch&#39;</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="s1">&#39;original_dec_tree_batch&#39;</span><span class="p">]</span>
            <span class="n">eval_input_graph</span> <span class="o">=</span> <span class="n">eval_input_graph</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">)</span>
            <span class="n">oov_dict</span> <span class="o">=</span> <span class="n">prepare_oov</span><span class="p">(</span><span class="n">eval_input_graph</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">src_vocab</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">device</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">use_copy</span><span class="p">:</span>
                <span class="n">reference</span> <span class="o">=</span> <span class="n">oov_dict</span><span class="o">.</span><span class="n">get_symbol_idx_for_list</span><span class="p">(</span><span class="n">batch_original_tree_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
                <span class="n">eval_vocab</span> <span class="o">=</span> <span class="n">oov_dict</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">reference</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">tgt_vocab</span><span class="o">.</span><span class="n">get_symbol_idx_for_list</span><span class="p">(</span><span class="n">batch_original_tree_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
                <span class="n">eval_vocab</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tgt_vocab</span>

            <span class="n">candidate</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">eval_input_graph</span><span class="p">,</span>
                                        <span class="n">oov_dict</span><span class="o">=</span><span class="n">oov_dict</span><span class="p">,</span>
                                        <span class="n">use_beam_search</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                        <span class="n">beam_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">opt</span><span class="p">[</span><span class="s2">&quot;beam_size&quot;</span><span class="p">])</span>

            <span class="n">candidate</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">candidate</span><span class="p">]</span>
            <span class="n">num_left_paren</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="mi">1</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">candidate</span> <span class="k">if</span> <span class="n">eval_vocab</span><span class="o">.</span><span class="n">idx2symbol</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="p">)]</span> <span class="o">==</span> <span class="s2">&quot;(&quot;</span><span class="p">)</span>
            <span class="n">num_right_paren</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="mi">1</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">candidate</span> <span class="k">if</span> <span class="n">eval_vocab</span><span class="o">.</span><span class="n">idx2symbol</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">c</span><span class="p">)]</span> <span class="o">==</span> <span class="s2">&quot;)&quot;</span><span class="p">)</span>
            <span class="n">diff</span> <span class="o">=</span> <span class="n">num_left_paren</span> <span class="o">-</span> <span class="n">num_right_paren</span>
            <span class="k">if</span> <span class="n">diff</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">diff</span><span class="p">):</span>
                    <span class="n">candidate</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data_loader</span><span class="o">.</span><span class="n">tgt_vocab</span><span class="o">.</span><span class="n">symbol2idx</span><span class="p">[</span><span class="s2">&quot;)&quot;</span><span class="p">])</span>
            <span class="k">elif</span> <span class="n">diff</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">candidate</span> <span class="o">=</span> <span class="n">candidate</span><span class="p">[:</span><span class="n">diff</span><span class="p">]</span>
            <span class="n">ref_str</span> <span class="o">=</span> <span class="n">convert_to_string</span><span class="p">(</span><span class="n">reference</span><span class="p">,</span> <span class="n">eval_vocab</span><span class="p">)</span>
            <span class="n">cand_str</span> <span class="o">=</span> <span class="n">convert_to_string</span><span class="p">(</span><span class="n">candidate</span><span class="p">,</span> <span class="n">eval_vocab</span><span class="p">)</span>
            <span class="n">reference_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reference</span><span class="p">)</span>
            <span class="n">candidate_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">candidate</span><span class="p">)</span>
        <span class="n">eval_acc</span> <span class="o">=</span> <span class="n">compute_tree_accuracy</span><span class="p">(</span><span class="n">candidate_list</span><span class="p">,</span> <span class="n">reference_list</span><span class="p">,</span> <span class="n">eval_vocab</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2"> accuracy = </span><span class="si">{:.3f}</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mode</span><span class="p">,</span> <span class="n">eval_acc</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">eval_acc</span>
</pre></div>
</div>
</div>
<div class="section" id="run-and-get-results">
<h2>Run and get results<a class="headerlink" href="#run-and-get-results" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">Mawps</span><span class="p">(</span><span class="n">cfg_g2t</span><span class="p">)</span>
<span class="n">best_acc</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">train</span><span class="p">()</span>
</pre></div>
</div>
<table class="docutils align-default">
<colgroup>
<col style="width: 39%" />
<col style="width: 26%" />
<col style="width: 19%" />
<col style="width: 16%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Graph construction</p></th>
<th class="head"><p>GNN embedding</p></th>
<th class="head"><p>Model</p></th>
<th class="head"><p>Accuracy</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Dependency graph</p></td>
<td><p>Graphsage</p></td>
<td><p>Graph2tree</p></td>
<td><p>78.0</p></td>
</tr>
</tbody>
</table>
</div>
</div>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="semantic_parsing.html" class="btn btn-neutral float-left" title="Semantic Parsing Tutorial" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="knowledge_graph_completion.html" class="btn btn-neutral float-right" title="Knowledge Graph Completion Tutorial" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2020, Graph4AI Group.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>